<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>实践一下--Docker基础版 | Celts</title><meta name="author" content="PaulGeorge"><meta name="copyright" content="PaulGeorge"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="每天一篇面试小知识  本篇着重介绍一下 Docker (基础)        写在前面 what is docker？ Docker 是一个基于 Go 语言开源的应用容器引擎。可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。">
<meta property="og:type" content="article">
<meta property="og:title" content="实践一下--Docker基础版">
<meta property="og:url" content="https://curry3035.gitee.io/posts/59617/index.html">
<meta property="og:site_name" content="Celts">
<meta property="og:description" content="每天一篇面试小知识  本篇着重介绍一下 Docker (基础)        写在前面 what is docker？ Docker 是一个基于 Go 语言开源的应用容器引擎。可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://curry3035.gitee.io/img/avatar.jpg">
<meta property="article:published_time" content="2022-01-07T01:00:00.000Z">
<meta property="article:modified_time" content="2022-05-26T18:16:56.011Z">
<meta property="article:author" content="PaulGeorge">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://curry3035.gitee.io/img/avatar.jpg"><link rel="shortcut icon" href="/img/ic.ico"><link rel="canonical" href="https://curry3035.gitee.io/posts/59617/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: PaulGeorge","link":"链接: ","source":"来源: Celts","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-right"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '实践一下--Docker基础版',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-27 02:16:56'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/background.css"><link rel="stylesheet" href="/css/my.css"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Celts"><span class="site-name">Celts</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">实践一下--Docker基础版</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-07T01:00:00.000Z" title="发表于 2022-01-07 09:00:00">2022-01-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-26T18:16:56.011Z" title="更新于 2022-05-27 02:16:56">2022-05-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%AE%B9%E5%99%A8/">容器</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="实践一下--Docker基础版"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>每天一篇面试小知识</p>
</blockquote>
<p><strong>本篇着重介绍一下 Docker (基础)</strong></p>
<hr>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Docker_logo.png" style="zoom: 6%;" />





<p><strong>写在前面</strong></p>
<p><font color=#008000>what is docker？</font></p>
<p>Docker 是一个基于 Go 语言开源的应用容器引擎。可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。</p>
<span id="more"></span>



<h3 id="Docker-架构"><a href="#Docker-架构" class="headerlink" title="Docker 架构"></a>Docker 架构</h3><h4 id="Docker-基本概念"><a href="#Docker-基本概念" class="headerlink" title="Docker 基本概念"></a>Docker 基本概念</h4><ol>
<li><p><font color=#6AAFE6> 镜像(Image)</font>：Docker 镜像（Image），就相当于是一个 root 文件系统。</p>
<p>比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。</p>
</li>
<li><p><font color=#6AAFE6>容器(Container)</font>：镜像（Image）和容器（Container）的关系，就像是面向对象程序设计中的类和实例一样，镜像是静态的定义，容器是镜像运行时的实体。</p>
<p>容器可以被创建、启动、停止、删除、暂停等。</p>
</li>
<li><p><font color=#6AAFE6>仓库(Repository)</font>：仓库可看成一个代码控制中心，用来保存镜像。</p>
</li>
</ol>
<blockquote>
<p>Docker 容器通过 Docker 镜像来创建</p>
</blockquote>
<p><strong>架构如图</strong></p>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2022-01-07_22-12-53.png"></p>
<hr>
<h3 id="Docker-基本命令"><a href="#Docker-基本命令" class="headerlink" title="Docker 基本命令"></a>Docker 基本命令</h3><h4 id="手动安装-默认是root权限"><a href="#手动安装-默认是root权限" class="headerlink" title="手动安装(默认是root权限)"></a>手动安装(默认是root权限)</h4><ol>
<li><p><strong>卸载旧版本</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum remove docker \</span><br><span class="line">                  docker-client \</span><br><span class="line">                  docker-client-latest \</span><br><span class="line">                  docker-common \</span><br><span class="line">                  docker-latest \</span><br><span class="line">                  docker-latest-logrotate \</span><br><span class="line">                  docker-logrotate \</span><br><span class="line">                  docker-engine</span><br></pre></td></tr></table></figure></li>
<li><p><strong>设置存储库</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum install -y yum-utils</span><br></pre></td></tr></table></figure></li>
<li><p><strong>阿里云源地址</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum-config-manager \</span><br><span class="line">    --add-repo \</span><br><span class="line">    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span><br></pre></td></tr></table></figure></li>
<li><p><strong>安装 Docker 引擎</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum install docker-ce docker-ce-cli containerd.io</span><br></pre></td></tr></table></figure></li>
<li><p><strong>启动docker</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl start docker</span><br></pre></td></tr></table></figure></li>
<li><p><strong>关闭docker</strong></p>
   <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl stop docker</span><br></pre></td></tr></table></figure></li>
<li><p><strong>重启docker</strong></p>
   <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure></li>
<li><p><strong>设置开机自启docker:</strong></p>
   <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl <span class="built_in">enable</span> docker</span><br></pre></td></tr></table></figure></li>
<li><p><strong>查看docker的状态</strong></p>
   <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl status docker</span><br></pre></td></tr></table></figure></li>
<li><p><strong>查看是否安装成功</strong></p>
 <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker -v</span><br></pre></td></tr></table></figure>

</li>
</ol>
<hr>
<h3 id="Docker-三要素"><a href="#Docker-三要素" class="headerlink" title="Docker 三要素"></a>Docker 三要素</h3><h4 id="镜像-image"><a href="#镜像-image" class="headerlink" title="镜像 (image)"></a>镜像 (image)</h4><ol>
<li><p><strong>列出主机上的镜像</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure></li>
<li><p><strong>列出本地的所有镜像</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker images -a</span><br></pre></td></tr></table></figure></li>
<li><p><strong>只显示镜像id</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker images -q</span><br></pre></td></tr></table></figure></li>
<li><p><strong>查询某个镜像</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker search xxx</span><br></pre></td></tr></table></figure></li>
<li><p><strong>查询某个镜像(取前5个)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker search --<span class="built_in">limit</span> 5 xxx</span><br></pre></td></tr></table></figure></li>
<li><p><strong>下载镜像(:tag 指定版本) [不加tag默认lasted取最新的镜像]</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker pull xxx:tag / docker pull xxx</span><br></pre></td></tr></table></figure></li>
<li><p><strong>查看镜像/容器/数据卷 所占用的空间</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker system <span class="built_in">df</span></span><br></pre></td></tr></table></figure></li>
<li><p><strong>删除镜像(xxx 可以是name也可以是image_id)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker rmi xxx</span><br></pre></td></tr></table></figure></li>
<li><p><strong>删除镜像(强制)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker rmi -f xxx</span><br></pre></td></tr></table></figure></li>
</ol>
<p><strong>运行模式</strong></p>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2022-01-08_02-48-47.png" style="zoom: 80%;" />

<hr>
<h4 id="容器-contain"><a href="#容器-contain" class="headerlink" title="容器 (contain)"></a>容器 (contain)</h4><p><strong>明确一点</strong></p>
<ul>
<li>docker是运行在linux内核上的！</li>
<li>启动守护式(后台运行)</li>
<li>docker中的虚悬镜像 – repository、tag 都为 &lt; none &gt; 的镜像 (建议删除！)</li>
</ul>
<ol>
<li><p><strong>启动一个容器(镜像实例)</strong></p>
 <figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run images </span><br></pre></td></tr></table></figure></li>
<li><p><strong>启动一个交互式容器(镜像实例)(前端有伪终端, 说白了就是有个bash交互命令平台)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -it images </span><br><span class="line"></span><br><span class="line">-P 随机(端口映射) </span><br><span class="line">-p 指定(端口映射) [8080:8080]==&gt;[外部访问docker端口8080映射到里面的容器实例端口8080]</span><br><span class="line"></span><br><span class="line">eg：</span><br><span class="line">docker run -it --name=my_ubuntu ubuntu /bin/bash </span><br><span class="line"></span><br><span class="line">退出终端</span><br><span class="line"><span class="built_in">exit</span>; 		(容器停止)</span><br><span class="line">ctrl+p+q 	(容器不停止)</span><br></pre></td></tr></table></figure>

<br/></li>
<li><p><strong>列出所有已启动的容器实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps</span><br></pre></td></tr></table></figure></li>
<li><p><strong>列出所有容器实例(已启动的和未启动的)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps -a</span><br></pre></td></tr></table></figure></li>
<li><p><strong>启动容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker start xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>重启容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker restart xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>停止容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker stop xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>强制停止容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">kill</span> xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>删除已停止的容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>强制删除已停止的容器(id)(name)</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> -f xxxxxxxxxx(<span class="built_in">id</span>)(name)</span><br></pre></td></tr></table></figure></li>
</ol>
<hr>
<ol>
<li><p><strong>守护进程模式(返回一个容器id)[如果是ubuntu这种操作系统的话，就需要我们使用 -it 这种形式] -itd</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d ubuntu</span><br><span class="line"></span><br><span class="line">docker run -itd --name ubuntu-test ubuntu /bin/bash</span><br></pre></td></tr></table></figure></li>
<li><p><strong>进入已经启动的容器, 退出容器还继续运行</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it xxxxxxxxxx /bin/bash </span><br><span class="line"></span><br><span class="line">退出终端(因为使用的是<span class="built_in">exec</span>)</span><br><span class="line"><span class="built_in">exit</span> 		(容器不停止)</span><br><span class="line">ctrl+p+q 	(容器不停止)</span><br></pre></td></tr></table></figure></li>
<li><p><strong>容器文件备份到主机上</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">cp</span> xxxxxxxxxx(<span class="built_in">id</span>):/xx/xx.txt /xx/xxx.txt</span><br></pre></td></tr></table></figure></li>
<li><p><strong>容器(整个)导出到主机上</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">export</span> xxxxxxxxxx &gt; xxx.tar</span><br></pre></td></tr></table></figure></li>
<li><p><strong>容器(整个)导入到主机上</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">cat</span> xxx.tar | docker import -xxx/xxx:1.0</span><br></pre></td></tr></table></figure></li>
<li><p>更新包管理工具</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">apt-get update</span><br><span class="line"></span><br><span class="line"><span class="comment">#安装(vim)</span></span><br><span class="line">apt-get install vim</span><br></pre></td></tr></table></figure></li>
<li><p>提交新的容器</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker commit -m=<span class="string">&quot;xxxxx&quot;</span> -a=<span class="string">&quot;zhangsan&quot;</span> xxxxxxxxxx(<span class="built_in">id</span>) linux/ubuntu_vim:1.0</span><br><span class="line">[docker commit -m=<span class="string">&quot;unbutu have vim&quot;</span> -a=<span class="string">&quot;yuha&quot;</span> 19654dd238a4 linux/ubuntu_vim:1.0]</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="容器卷-container-volume"><a href="#容器卷-container-volume" class="headerlink" title="容器卷 (container volume)"></a>容器卷 (container volume)</h4><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2022-01-10_00-17-55.png" style="zoom:67%;" />

<ol>
<li><p><strong>数据卷</strong></p>
<p>挂载 (mount): docker挂载就是用本机文件或文件夹覆盖容器内的文件或文件夹;</p>
</li>
<li><p><strong>容器卷和主机互通互联</strong></p>
<p>[docker run -it –privileged=true -v /宿主机绝对路径目录:/容器内目录 –name=镜像别名 镜像名]</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -it --privileged=<span class="literal">true</span> -v /temp/host_data:/temp/docker_data --name=my_ubuntu ubuntu</span><br></pre></td></tr></table></figure></li>
<li><p><strong>privileged</strong></p>
<p>容器内的root拥有真正的root权限;</p>
<p>否则，容器内的root只是外部的一个普通用户权限;</p>
<p>privileged启动的容器，可以看到很多host上的设备，并且可以执行mount;</p>
</li>
</ol>
<p><strong>注意</strong>：如果实现容器卷和主机互通互联，即使容器stop，主机上与容器卷之间互通的文件部分也会做相应的同步。</p>
<hr>
<h3 id="Docker-常用软件安装"><a href="#Docker-常用软件安装" class="headerlink" title="Docker 常用软件安装"></a>Docker 常用软件安装</h3><h4 id="Apache-Tomcat"><a href="#Apache-Tomcat" class="headerlink" title="Apache Tomcat"></a>Apache Tomcat</h4><ol>
<li><p>pull</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker pull tomcat</span><br></pre></td></tr></table></figure></li>
<li><p>run 指定外部端口映射8080</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d -p 8080:8080 --name my_tomcat tomcat</span><br></pre></td></tr></table></figure></li>
<li><p>访问8080</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">ip:8080</span><br></pre></td></tr></table></figure></li>
<li><p>可能出现的问题：访问出现404</p>
<p>新版本 tomcat 的 webapp 是空的，新增了文件 webapps.dist，需要我们删除掉webapps，并重命名文件 webapps.dist 为 webapps ;</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">rm</span> -rf webapps</span><br><span class="line"><span class="built_in">mv</span> webapps.dist webapps</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="MySQL"><a href="#MySQL" class="headerlink" title="MySQL"></a>MySQL</h4><ol>
<li><p>pull</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker pull mysql:5.7</span><br></pre></td></tr></table></figure></li>
<li><p>run</p>
<p>先查看端口号3306是否被占用</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">ps -ef | grep mysql</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -p 3306:3306 --name my_mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7</span><br></pre></td></tr></table></figure>

<hr>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2022-01-10_01-14-48.png" style="zoom: 67%;" /></li>
</ol>
<hr>
<p><strong>注意</strong>：docker中的mysql字符自是默认拉丁，不是UTF-8</p>
<h4 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h4><ol>
<li><p>pull</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker pull redis:6.0.8</span><br></pre></td></tr></table></figure></li>
<li><p>run</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">run -d -p 6379:6379 --name my_redis redis:6.0.8</span><br></pre></td></tr></table></figure></li>
</ol>
<br/>

<p><strong>未完待续 … …</strong></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io">PaulGeorge</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io/posts/59617/">https://curry3035.gitee.io/posts/59617/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://curry3035.gitee.io" target="_blank">Celts</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Docker/">Docker</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/37173/" title="实践一下-- Maven依赖冲突"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">实践一下-- Maven依赖冲突</div></div></a></div><div class="next-post pull-right"><a href="/posts/56511/" title="实践一下--MySQL 优化"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">实践一下--MySQL 优化</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">PaulGeorge</div><div class="author-info__description">很多事情就像是旅行一样，当你决定要出发的时候，最困难的那部分其实就已经完成了</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/PaulGeorge123"><i class="fab fa-github"></i><span>GitHub URL</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/PaulGeorge123" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-%E6%9E%B6%E6%9E%84"><span class="toc-number">1.</span> <span class="toc-text">Docker 架构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Docker-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.1.</span> <span class="toc-text">Docker 基本概念</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-%E5%9F%BA%E6%9C%AC%E5%91%BD%E4%BB%A4"><span class="toc-number">2.</span> <span class="toc-text">Docker 基本命令</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%AE%89%E8%A3%85-%E9%BB%98%E8%AE%A4%E6%98%AFroot%E6%9D%83%E9%99%90"><span class="toc-number">2.1.</span> <span class="toc-text">手动安装(默认是root权限)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-%E4%B8%89%E8%A6%81%E7%B4%A0"><span class="toc-number">3.</span> <span class="toc-text">Docker 三要素</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%95%9C%E5%83%8F-image"><span class="toc-number">3.1.</span> <span class="toc-text">镜像 (image)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8-contain"><span class="toc-number">3.2.</span> <span class="toc-text">容器 (contain)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%8D%B7-container-volume"><span class="toc-number">3.3.</span> <span class="toc-text">容器卷 (container volume)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85"><span class="toc-number">4.</span> <span class="toc-text">Docker 常用软件安装</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Apache-Tomcat"><span class="toc-number">4.1.</span> <span class="toc-text">Apache Tomcat</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#MySQL"><span class="toc-number">4.2.</span> <span class="toc-text">MySQL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Redis"><span class="toc-number">4.3.</span> <span class="toc-text">Redis</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/47231/" title="POI读取Excel问题">POI读取Excel问题</a><time datetime="2023-04-11T01:00:00.000Z" title="发表于 2023-04-11 09:00:00">2023-04-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/8422/" title="Excel大文件的上传">Excel大文件的上传</a><time datetime="2023-04-10T01:00:00.000Z" title="发表于 2023-04-10 09:00:00">2023-04-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/55119/" title="每日一面--Files工具类">每日一面--Files工具类</a><time datetime="2023-01-01T01:00:00.000Z" title="发表于 2023-01-01 09:00:00">2023-01-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/34600/" title="面试一下--JUC入门">面试一下--JUC入门</a><time datetime="2022-09-10T01:00:00.000Z" title="发表于 2022-09-10 09:00:00">2022-09-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/16284/" title="每日一面--字符流和字节流">每日一面--字符流和字节流</a><time datetime="2022-07-01T01:00:00.000Z" title="发表于 2022-07-01 09:00:00">2022-07-01</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By PaulGeorge</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>